namespace hjc {

	export class LayerManager extends Singleton implements ILaunchable {

		private static INFO_LIST = '信息列表';
		private $sceneLayer: eui.Group;		//场景层(皮肤/交互)
		private $menuLayer: eui.Group;		//通用菜单层(皮肤/交互)
		private $panelLayer: eui.Group;		//面板层(皮肤组件/交互)
		private $dialogLayer: eui.Group;	//对话层(皮肤组件/交互)	
		private $effectLayer: eui.Group;	//特效层(皮肤组件/可模态)
		private $infoLayer: eui.Group;		//信息层
		private $coverLayer: eui.Group;		//遮罩层(模态)
		private $all: eui.Group[];			//所有层
		public get scene(): eui.Group { return this.$sceneLayer; }
		public get menu(): eui.Group { this.$menuLayer.visible = true; return this.$menuLayer; }
		public get panel(): eui.Group { this.$panelLayer.visible = true; return this.$panelLayer; }
		public get dialog(): eui.Group { this.$dialogLayer.visible = true; return this.$dialogLayer; }
		public get effect(): eui.Group { this.$effectLayer.visible = true; return this.$effectLayer; }
		public get info(): eui.Group { this.$infoLayer.visible = true; return this.$infoLayer; }
		public get cover(): eui.Group { this.$coverLayer.visible = true; return this.$coverLayer; }
		public getInfoList(bottom?: number): eui.Group {
			let list = this.$infoLayer.getChildByName(LayerManager.INFO_LIST) as eui.Group;
			if (numeric(bottom)) list.bottom = bottom; this.$infoLayer.visible = true; return list;
		}

		public launch(options: LaunchOptions): void {
			this.$sceneLayer = this.addLayer('场景主层');
			this.$sceneLayer.visible = true;
			this.$menuLayer = this.addLayer('菜单层', true);
			this.$panelLayer = this.addLayer('面板层', true);
			this.$dialogLayer = this.addLayer('对话层', true);
			this.$effectLayer = this.addLayer('特效层', true, false);
			this.$infoLayer = this.addLayer('信息层', true, false);
			this.$coverLayer = this.addLayer('遮罩层', false, true);
			this.$all = [this.$coverLayer, this.$infoLayer, this.$effectLayer,
			this.$dialogLayer, this.$panelLayer, this.$menuLayer, this.$sceneLayer];
			this.initInfoList();
			if (!options.configs.showAll) this.resizeAll();
		}

		//初始化信息层弹信息列表
		private initInfoList(): void {
			let [list, layout] = [new eui.Group(), new eui.VerticalLayout()];
			layout.horizontalAlign = egret.HorizontalAlign.CENTER; layout.gap = 15;
			ui.setCenter(list, this.$infoLayer, { vc: 200 });
			list.name = LayerManager.INFO_LIST; list.layout = layout;
		}

		//为游戏主容器加入一个指定参数的层
		private addLayer(name: string, touchThrough: boolean = false, touchEnabled: boolean = true): eui.Group {
			let showAll = game.showAll;
			let layer = new eui.Group();
			game.container.addChildAt(layer, game.container.numChildren - 4);
			this._resize(layer);
			layer.name = name;
			layer.visible = false;
			layer.touchEnabled = touchEnabled;
			layer.touchThrough = touchThrough;
			return layer;
		}

		private _resize(layer: eui.Group): void {
			ui.setDisplay(layer, { x: game.paddingLR, y: game.paddingTB, width: game.width, height: game.height });
			ui.setScale(layer, game.scale);
		}

		public resizeAll(): void {
			this.$all.forEach(layer => this._resize(layer));
		}
	}
}